angular.module('app-component-directive', [])

.directive 'wfSrc', (cdn) ->
    restrict: 'A'
    link: (scope, element, attr) ->
        if cdn == null
            element.attr 'src', attr.wfSrc
        else
            element.attr 'src', cdn + '/' + attr.wfSrc
        element.attr 'wf-src', null

.directive 'ygjSrc', (cdn) ->
    restrict: 'A'
    link: (scope, element, attr) ->
        attr.$observe 'ygjSrc', ->
            console.log 'enter'
            if cdn == null
                element.attr 'src', attr.ygjSrc
            else
                element.attr 'src', cdn + '/' + attr.ygjSrc

.service 'Toast', ($ionicModal, $rootScope) ->
    scope = $rootScope.$new()

    modal = $ionicModal.fromTemplate("
        <div id='toastModal' style='width: 100%;height: 800px;background: rgba(0,0,0,.5);'>
        <div style='
        padding: 10px;
        width: 50%;
        height: auto;
        background-color: #fff;
        border-radius: 5px;
        text-align: center;
        position: absolute;
        left: 50%;
        top:50%;
        transform: translate(-50%,-50%);
        -ms-transform: translate(-50%,-50%);
        -moz-transform: translate(-50%,-50%);
        -webkit-transform: translate(-50%,-50%);
        -o-transform: translate(-50%,-50%);
        font-size: 14px;'>
        <img wf-src='img/info.png' style='width: 20px;'/>
        <p ng-bind='msg'></p>
        </div>
        </div>",
        {
            scope: scope
            backdropClickToClose: false
            hardwareBackButtonClose: false
        })
    return {
        showMsg: (msg, time = 1000)->
            scope.msg = msg
            modal.show()
            if time != -1
                _.delay (->
                    modal.hide()
                ), time
        showSysError: (msg = '网络异常,请稍后重试', time = 1000)->
            scope.msg = msg
            modal.show()
            if time != -1
                _.delay (->
                    modal.hide()
                ), time
        showBusinessError: (resp, time = 1000)->
            console.log resp
            if resp.status == 0
                errorMsg = resp.body.reason
            else
                errorMsg = resp.error
            scope.msg = errorMsg
            modal.show()
            if time != -1
                _.delay (->
                    modal.hide()
                ), time
    }
